(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.b.assetrepayment.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'assetrepaymentService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, assetrepaymentService) {
		var ctrl = this, params = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("assetrepaymentList"),errorMessage = {};;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		if(params.assetId && params.assetId != 0){
			ctrl.assetIdReadonly = true;
		}
		
		if(!ctrl.assetIdReadonly){
			angular.forEach(layoutConfig.buttonConfigs.buttonGroup.buttons, function(button, key) {
				if(button.name == "back"){
					layoutConfig.buttonConfigs.buttonGroup.buttons.splice(key,1);
				}
			});
		}
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			
			assetrepaymentService.getAssetrepaymentListOptions().then(function(data){
				ctrl.assetrepayStatusOptions = data.assetrepaymentStatus;
				ctrl.assetrepayCcyOptions = data.currency;
				ctrl.yesOrNoOptions = data.yesOrNo;
				ctrl.paySrcOptions= data.paySrc;
				ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
				ctrl.searchCriteria.assetId = params.assetId;
				ctrl.search();
			});
		};
		
		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('apf.b.assetrepayment.view', ctrl.selectedRows[0]);
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
		
		ctrl.viewAssetRepayment = function(row) {
			$scope.go('apf.b.assetrepaymentrepayment.list', {"assetrepaymentId": row.assetrepaymentId});
		};
		
		ctrl.viewInvoice = function(row) {
			$scope.go('apf.b.invoice.list', {"assetrepaymentId": row.assetrepaymentId});
		};
		
	}
})(angular);
